﻿namespace REVISIONMAX20
{
    using MySql.Data.MySqlClient;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    public class frmConnectionString : Form
    {
        private UIHandler _uiHandler = null;
        private ToolStripButton btnApply;
        private ToolStripButton btnSave;
        private ToolStripButton btnTest;
        private IContainer components = null;
        private ConnectionString connectionstring = null;
        private MySqlConnectionStringBuilder connectionStringBuilder = null;
        private Label label1;
        private Label label3;
        private Label label4;
        private Label label7;
        private Label label8;
        private Label label9;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStrip tsMain;
        private MyTextBox txtDataBase;
        private MyTextBox txtName;
        private MyTextBox txtPassword;
        private MyTextBox txtPort;
        private MyTextBox txtServer;
        private MyTextBox txtUserID;

        public frmConnectionString()
        {
            this.InitializeComponent();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            this.UpdateObject();
            Exception exception = this.connectionstring.TestConnection();
            if (exception == null)
            {
                MessageBox.Show("Test Successful.");
            }
            else
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.txtDataBase = new REVISIONMAX20.MyTextBox(this.components);
            this.label3 = new System.Windows.Forms.Label();
            this.txtServer = new REVISIONMAX20.MyTextBox(this.components);
            this.label4 = new System.Windows.Forms.Label();
            this.txtPort = new REVISIONMAX20.MyTextBox(this.components);
            this.label7 = new System.Windows.Forms.Label();
            this.txtUserID = new REVISIONMAX20.MyTextBox(this.components);
            this.txtPassword = new REVISIONMAX20.MyTextBox(this.components);
            this.label8 = new System.Windows.Forms.Label();
            this.tsMain = new System.Windows.Forms.ToolStrip();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.btnApply = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.btnTest = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.txtName = new REVISIONMAX20.MyTextBox(this.components);
            this.label9 = new System.Windows.Forms.Label();
            this.tsMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(0, 90);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(80, 20);
            this.label1.TabIndex = 8;
            this.label1.Text = "DataBase";
            // 
            // txtDataBase
            // 
            this.txtDataBase.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtDataBase.ForeColor = System.Drawing.Color.Blue;
            this.txtDataBase.Location = new System.Drawing.Point(86, 90);
            this.txtDataBase.Name = "txtDataBase";
            this.txtDataBase.Size = new System.Drawing.Size(163, 20);
            this.txtDataBase.TabIndex = 2;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(0, 111);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(80, 20);
            this.label3.TabIndex = 7;
            this.label3.Text = "Server";
            // 
            // txtServer
            // 
            this.txtServer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtServer.ForeColor = System.Drawing.Color.Blue;
            this.txtServer.Location = new System.Drawing.Point(86, 111);
            this.txtServer.Name = "txtServer";
            this.txtServer.Size = new System.Drawing.Size(163, 20);
            this.txtServer.TabIndex = 3;
            this.txtServer.Text = "mikedb01";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(0, 132);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(80, 20);
            this.label4.TabIndex = 6;
            this.label4.Text = "Port";
            // 
            // txtPort
            // 
            this.txtPort.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtPort.ForeColor = System.Drawing.Color.Blue;
            this.txtPort.Location = new System.Drawing.Point(86, 132);
            this.txtPort.Name = "txtPort";
            this.txtPort.Size = new System.Drawing.Size(163, 20);
            this.txtPort.TabIndex = 4;
            this.txtPort.Text = "3306";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(0, 175);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(80, 20);
            this.label7.TabIndex = 5;
            this.label7.Text = "Password";
            // 
            // txtUserID
            // 
            this.txtUserID.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtUserID.ForeColor = System.Drawing.Color.Blue;
            this.txtUserID.Location = new System.Drawing.Point(86, 153);
            this.txtUserID.Name = "txtUserID";
            this.txtUserID.Size = new System.Drawing.Size(163, 20);
            this.txtUserID.TabIndex = 5;
            this.txtUserID.Text = "root";
            // 
            // txtPassword
            // 
            this.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtPassword.ForeColor = System.Drawing.Color.Blue;
            this.txtPassword.Location = new System.Drawing.Point(86, 175);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.Size = new System.Drawing.Size(163, 20);
            this.txtPassword.TabIndex = 6;
            this.txtPassword.Text = "root";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(0, 153);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(80, 20);
            this.label8.TabIndex = 4;
            this.label8.Text = "User ID";
            // 
            // tsMain
            // 
            this.tsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnSave,
            this.toolStripSeparator4,
            this.btnApply,
            this.toolStripSeparator3,
            this.btnTest,
            this.toolStripSeparator2});
            this.tsMain.Location = new System.Drawing.Point(0, 0);
            this.tsMain.Name = "tsMain";
            this.tsMain.Size = new System.Drawing.Size(282, 25);
            this.tsMain.TabIndex = 16;
            this.tsMain.Text = "toolStrip1";
            // 
            // btnSave
            // 
            this.btnSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(35, 22);
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 25);
            // 
            // btnApply
            // 
            this.btnApply.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnApply.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnApply.Name = "btnApply";
            this.btnApply.Size = new System.Drawing.Size(38, 22);
            this.btnApply.Text = "Apply";
            this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // btnTest
            // 
            this.btnTest.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnTest.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnTest.Name = "btnTest";
            this.btnTest.Size = new System.Drawing.Size(32, 22);
            this.btnTest.Text = "Test";
            this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // txtName
            // 
            this.txtName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtName.ForeColor = System.Drawing.Color.Blue;
            this.txtName.Location = new System.Drawing.Point(86, 40);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(163, 20);
            this.txtName.TabIndex = 0;
            this.txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(0, 40);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(80, 20);
            this.label9.TabIndex = 8;
            this.label9.Text = "Name";
            // 
            // frmConnectionString
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(282, 291);
            this.Controls.Add(this.tsMain);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.txtDataBase);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtServer);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtPort);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.txtUserID);
            this.Controls.Add(this.txtPassword);
            this.Controls.Add(this.label8);
            this.Name = "frmConnectionString";
            this.tsMain.ResumeLayout(false);
            this.tsMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Save(bool apply)
        {
            this.UpdateObject();
            StringBuilder builder = this.connectionstring.SaveOrUpdate();
            if (builder.Length > 0)
            {
                MessageBox.Show(builder.ToString());
            }
            else if (!apply)
            {
                base.Close();
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            this.Text = "Connection String: " + this.txtName.Text;
        }

        private void UpdateObject()
        {
            this.connectionstring.name = this.txtName.Text;
            this.connectionStringBuilder.Database = this.txtDataBase.Text;
            this.connectionStringBuilder.Server = this.txtServer.Text;
            this.connectionStringBuilder.Port = this.txtPort.Text.UIntValueOrDefault(0xe8c);
            this.connectionStringBuilder.UserID = this.txtUserID.Text;
            this.connectionStringBuilder.Password = this.txtPassword.Text;
            this.connectionstring.value = this.connectionStringBuilder.ToString();
        }

        private void UpdateViewer()
        {
            this.txtName.Text = this.connectionstring.name;
            this.connectionStringBuilder = this.connectionstring.ConnectionStringBuilder;
            if (this.connectionstring.Status == Status.StatusNew)
            {
                this.txtDataBase.Text = this.connectionstring.name;
            }
            else
            {
                this.txtDataBase.Text = this.connectionStringBuilder.Database;
                this.txtServer.Text = this.connectionStringBuilder.Server;
                this.txtPort.Text = this.connectionStringBuilder.Port.StringValueOrEmpty();
                this.txtUserID.Text = this.connectionStringBuilder.UserID;
                this.txtPassword.Text = this.connectionStringBuilder.Password;
            }
        }

        public void View(UIHandler puiHandler, ConnectionString pConnectionString)
        {
            this._uiHandler = puiHandler;
            this.connectionstring = pConnectionString;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public int id
        {
            get
            {
                return this.connectionstring.id;
            }
        }
    }
}

